package com.resou.reader.discovery;

import com.resou.reader.R;
import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.ActivityBean;
import com.resou.reader.api.entry.DisCoveryBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class HomeDiscoveryPresenter extends ResouBasePresenter<IDiscoveryContract.IDiscoveryView> implements IDiscoveryContract.IDiscoveryPresenter {
    StoreService storeService;

    public HomeDiscoveryPresenter(IDiscoveryContract.IDiscoveryView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    @Override
    public void quearData() {
        mView.showProgress();
        storeService.quearDiscoveryData(UserUtil.getToken(), "find")
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<DisCoveryBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<DisCoveryBean>> listResult) {
                        listResult.getData().add(1, new DisCoveryBean(20, R.mipmap.ic_choicene, "包月VIP", true));
                        mView.showData(listResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showEmpty();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void quearActivityInfo() {
        storeService.quearDiscoveryActivity(UserUtil.getToken())
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<List<ActivityBean>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<List<ActivityBean>> listResult) {
                        if (listResult.getData() == null || listResult.getData().size() == 0) {
                            mView.showAcitivty(null);
                        } else {
                            mView.showAcitivty(listResult.getData().get(0));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
